My journey of C has begun [8]

  • 2020-04-01 21:27:28
  • OfStack

19. Basic data types: complex Numbers and imaginary Numbers

C99 added The plural types ( _Complex ) and Imaginary type ( _Imaginary ). Simply put, C99 provides three complex types: Float _Complex . Double _Complex , and Long double _Complex . for Float _Complex For variables of type, it contains two float A value of type, one used to represent a complex number Real component ( Real part ), another one is used to express Imaginary part ( Imaginary part ). Similarly,
Double _Complex Consists of two A double Value of type. And so on. The C99 also provides three imaginary number types: Float _Imaginary . Double _Imaginary , as well as Long double _Imaginary . Imaginary number types have only imaginary parts and no real parts. Contains standard header files Complex. H After that, we can use it complex To represent the _Complex with
imaginary To represent the _Imaginary , as well as with i. To represent imaginary units i. , that is, - 1 Square root. Such as:       # include < Complex. H >
              Double _Complex x = 5.2; /* the real part is 5.2, and the imaginary part is 0 */
              Double complex y = 5.0 * I; /* the real part is 0, the imaginary part is 5.0 */
              Double complex z = 5.2 and 5.0 * I; /* the real part is 5.2 and the imaginary part is 5.0 */ Pay attention to : _Complex Type for Free-standing environment ( Freestanding environment ) is optional . Optional means that it is not mandatory to support this type. The so-called Free-standing environment C programs can run without an operating system. _Imaginary Types are optional in any environment. Neither type is well supported by current compilers, so I won't discuss them further here.       At this point, the basic data types are covered.


20. Escape character use instance
Here's an example \ a. , \ b , \ t , \ r , as well as \ n The role of. Among them \ b . \ t and \ r The concept dates back to when computers still used typewriters as output devices, so they may not work for some modern computers. On some Macintosh computers, they did something different. First take a good look at the following program, then compile and run it to see if the results are what you expect, then look at the details later. /* esc_sq.c -- use the escape character */& PI; # include < stdio.h >   Int main (void) {       Float height;         Printf (" \ aPlease enter your height: _____ cm \ b \ \ \ \ \ b b b b b b \ \ b ");       The scanf (" % f ", & height);       Printf (" \ tYour height is %. 2 f \ rOh! \ n ", height);         Return 0; } The first printf usually causes a sound ringing ( \ a. ) and display the following prompt Please enter your height: cm Where the active position (cursor) is at the beginning of the underscore. The active position is at the beginning of the underscore because of the eight \ b Moved back eight active positions. By the way, underline _____ Consists of five _ Composition. Pay attention to : in general, \b will not erase the previous characters, but in some systems, \b will erase the previous characters, resulting in the display result of Both Please enter your height: . Effect produced by \a It depends on the hardware . In general, the output \a will be generated ringing . However, in some systems, the output \a does not produce any effect, or just displays a special character.       After entering a number (such as 180.5), the display of the screen will become Please enter your height: 180.5 cm 

The number we entered replaced the original underscore. And then we have to press enter Determine our input. When I press enter,
The active position moves to the beginning of the next line.               After the second printf runs, the screen displays as follows:         Please enter your height: 180.5 cm       Oh!         Your height is 180.50 \ t Move the active position back a number of positions (usually eight), and then output
Your height is 180.50. . Then, \ r Returns the active position to the beginning of the current row, and then outputs Oh! .
The last \ n Causes a line break.  

 

  Addition operator Add the values of the left and right sides. For example:   Printf (" % d ", 9 + 11);   The output is 20. The operands of the addition operator can be either constants or variables. For example:   Var = var_1 + var_2;   This statement adds the value of var_1 to the value of var_2, and then assigns the sum to var.     2. Subtraction Operator: all   Subtraction operator Causes the left operand to subtract the right operand. For example:   Var = 100 and 40;   The value of 100 and 40 is 60, and then that 60 is assigned to var.   Addition operator and Subtraction operator Referred to as Binary operator ( Binary operator ), because they require two operands.     3. Sign Operator: both and +   For example:   Var_1 = 5; Var_2 = - var_1; Var = + 5   Var_1 is minus 5, var_2 is 5, var is 5.   The plus and minus operator is called Monocular operator ( Unary operator ), because they only need one operand.

 

 

The multiplication and division operators
1. The Multiplication Operator (Multiplication Operator) : *       * is the multiplication operator. The following statement:               Meter = 100 * cm; The constant 100 is multiplied by the variable cm, and the product is assigned to the variable meter.
2. Division Operator: /       C use / As a division operator. / The left operand divided by the right operand. In other words, / The left side is the dividend, and the right side is the divisor. Such as:               Var = 6/2; 6 divided by 2 is 3, and then 3 is assigned to the variable var.       Integer division is different from floating-point division. Divide floating-point Numbers to get floating-point Numbers, and divide integers to get integers. C language, the integer division of the fractional part is discarded, this is called truncation. For example, 7/4 is 1 instead of 1.75 or 2.       Now let's look at a little program.              
              # include < stdio.h >               Int main (void)
              {
                      Printf (" integer division:   7/4     Is % d \ n ", 7/4);
                      Printf ("floating division: 7./4. Is %f \n", 7./4.);
                      Printf (" mixed division:       7. / 4   Is % f \ n ", 7. / 4);                       Return 0;
              } The output is:               Integer division:   7/4     Is 1
              Floating division: 7./4. Is 1.750000
              Mixed division:       7. / 4   Is 1.750000 In the last printf, we divide the floating point by the integer, and we get the same result as in the second printf. This is because C automatically converts the types of the two operands to the same type. In this example, the integer 4 is converted to a floating point type and then divided by the floating point number 7.       Before C99, positive integers and negative integers were divided, and if the fractional part was generated, the result of the division was uncertain. For example, the result of 7/-4 could be -2 or -1. C99 provides that positive integer and negative integer divide, the resulting decimal part is discarded. In other words, in C99, 7 over negative 4 is definitely going to be negative 1.

Related articles: